var sb = {
	setting: {
		ajaxLoadTarget: 'body',
		debug: true
	},
	statusCode: {
		ok: 200,
		error: 300,
		timeout: 301
	},
	keys: {
		statusCode: "statusCode",
		message: "message"
	},
	debug: function(msg) {
		if(this.setting.debug) {
			if(typeof(console) != "undefined")
				console.log(msg);
			else
				alert(msg);
		}
	},
	jsonEval: function(data) {
		try {
			if($.type(data) == 'string')
				return eval('(' + data + ')');
			else
				return data;
		} catch(e) {
			return {};
		}
	},
	success: function(msg) {
		top.toastr.clear();
		top.toastr.success(msg || '操作成功');
	},
	error: function(msg) {
		top.toastr.clear();
		top.toastr.error(msg || '程序出错');
	},
	ajaxDone: function(data) {
		try {
			var json = jQuery.isPlainObject(data) ? data : jQuery.parseJSON(data.trim());
			if(json.statusCode == '200') {
				sb.success(json.message);

				if(json.callbackType) {
					if(json.callbackType == 'closeCurrent') {
						if(json.forwardUrl) {
							$('form', parent.window.document).attr('action', json.forwardUrl)
						}
						$('form', parent.window.document).submit();

						var index = parent.layer.getFrameIndex(window.name); // 先得到当前iframe层的索引
						parent.layer.close(index);
						// navTab.closeCurrentTab();
					} else if(json.callbackType == 'refresh') {
						$('form', document).submit();
					} else if(json.callbackType == 'forward') {
						window.location.href = json.forwardUrl;
					}
				}
				// 载入新页面
				if(json.navTabId) {
					// var tabid = json.navTabId.indexOf('?') < 0 ? json.navTabId : json.navTabId.substring(0, json.navTabId.indexOf('?'));
					// var index = navTab._indexTabId(tabid);
					//
					// var element = layui.element();
					// element.tabChange('navTab', index);
					// navTab.getPanel(tabid).find('form').eq(0).submit();
				}
			} else {
				sb.error(json.message);
			}
		} catch(e) {
			var $target = $('.layui-tab-content .layui-show');
			$target.html($(data)).initUI();
		}
	},
	ajaxError: function(data) {
		var json = sb.jsonEval(data);
		if(json.statusCode == '300') {

		} else {
			sb.error(json.message);
		}
	}
};

$.fn.extend({

	initUI: function() {
		return this.each(function() {
			if($.isFunction(initUI)) {
				initUI(this);
			}
		});
	}
});

/**
 * 扩展String方法
 */
$.extend(String.prototype, {
	isPositiveInteger: function() {
		return(new RegExp(/^[1-9]\d*$/).test(this));
	},
	isInteger: function() {
		return(new RegExp(/^\d+$/).test(this));
	},
	isNumber: function(value, element) {
		return(new RegExp(/^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/).test(this));
	},
	trim: function() {
		return this.replace(/(^\s*)|(\s*$)|\r|\n/g, "");
	},
	startsWith: function(pattern) {
		return this.indexOf(pattern) === 0;
	},
	endsWith: function(pattern) {
		var d = this.length - pattern.length;
		return d >= 0 && this.lastIndexOf(pattern) === d;
	},
	replaceSuffix: function(index) {
		return this.replace(/\[[0-9]+\]/, '[' + index + ']').replace('#index#', index);
	},
	trans: function() {
		return this.replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&quot;/g, '"');
	},
	encodeTXT: function() {
		return(this).replaceAll('&', '&amp;').replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll(" ", "&nbsp;");
	},
	replaceAll: function(os, ns) {
		return this.replace(new RegExp(os, "gm"), ns);
	},
	replaceTm: function($data) {
		if(!$data)
			return this;
		return this.replace(RegExp("({[A-Za-z_]+[A-Za-z0-9_]*})", "g"), function($1) {
			return $data[$1.replace(/[{}]+/g, "")];
		});
	},
	replaceTmById: function(_box) {
		var $parent = _box || $(document);
		return this.replace(RegExp("({[A-Za-z_]+[A-Za-z0-9_]*})", "g"), function($1) {
			var $input = $parent.find("#" + $1.replace(/[{}]+/g, ""));
			return $input.val() ? $input.val() : $1;
		});
	},
	isFinishedTm: function() {
		return !(new RegExp("{[A-Za-z_]+[A-Za-z0-9_]*}").test(this));
	},
	skipChar: function(ch) {
		if(!this || this.length === 0) {
			return '';
		}
		if(this.charAt(0) === ch) {
			return this.substring(1).skipChar(ch);
		}
		return this;
	},
	isValidPwd: function() {
		return(new RegExp(/^([_]|[a-zA-Z0-9]){6,32}$/).test(this));
	},
	isValidMail: function() {
		return(new RegExp(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/).test(this.trim()));
	},
	isSpaces: function() {
		for(var i = 0; i < this.length; i += 1) {
			var ch = this.charAt(i);
			if(ch != ' ' && ch != "\n" && ch != "\t" && ch != "\r") {
				return false;
			}
		}
		return true;
	},
	isPhone: function() {
		return(new RegExp(/(^([0-9]{3,4}[-])?\d{3,8}(-\d{1,6})?$)|(^\([0-9]{3,4}\)\d{3,8}(\(\d{1,6}\))?$)|(^\d{3,8}$)/).test(this));
	},
	isUrl: function() {
		return(new RegExp(/^[a-zA-z]+:\/\/([a-zA-Z0-9\-\.]+)([-\w .\/?%&=:]*)$/).test(this));
	},
	isExternalUrl: function() {
		return this.isUrl() && this.indexOf("://" + document.domain) == -1;
	},
	format: function(params) {
		var source = this;

		if(params.constructor != Array) {
			params = [
				params
			];
		}
		$.each(params, function(i, n) {
			source = source.replace(new RegExp("\\{" + i + "\\}", "g"), n);
		});
		return source;
	}
});